<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use app\admin\model\shop\Goods as GoodsModel;
/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Goods extends Api
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
    public function __construct(){
        $this->GoodsModel=new GoodsModel();
    }
    public function getGoodsLists(){
        $where=[
            ['is_shelves','=',1]
        ];
        $get=input('get.');
        if(isset($get['cate_id']) && $get['cate_id']){
            array_push($where,['cate_id','=',$get['cate_id']]);
        }
        $page=input('page');
        $size=input('size');
        $list=$this->GoodsModel->field('products_id,products_title,price,activity_price,cover_picture,main_picture,detail_picture,is_hot,unit')->where($where)->page($page,$size)->select();
        foreach ($list as $k => $v) {
           $list[$k]['main_picture']=explode('|',$v->main_picture);
           $list[$k]['detail_picture']=explode('|',$v->detail_picture);
            $list[$k]['sku']=Db::name('goods_value')->where('products_id',$v->products_id)->value('sku');
        }
        $this->result('success',$list,200,'json');
    }
    public function getShopDetials(){
         $data=input("get.");
        $rule = [
            'products_id|products_id参数'   => 'require|number',
        ];
        $result =$this->validate($data, $rule);
        $where=[
            ['products_id','=',$data['products_id']]
        ];
        if(isset($data['is_recommend'])){
            array_push($where, ['is_recommend','=',$data['is_recommend']]);
        }
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $res =$this->GoodsModel->where($where)->find();
        $res['sku']=Db::name('goods_value')->where('products_id',$data['products_id'])->value('sku');
        if($res->main_picture) $res['main_picture']=explode('|',$res->main_picture);
        if($res->detail_picture) $res['detail_picture']=explode('|',$res->detail_picture);
        if($res->spec_type=="more"){
            
            if($res->skus){
                $res['skus']=json_decode($res->skus,true);
            }
                $new_specs_value=$this->getSpecData($res->specs,json_decode($res->specs_value,true),$data['products_id']);
                $res->specs_value=$new_specs_value;
                $checked_skus='';
                $product_value= $this->get_check_sku($data['products_id'],$new_specs_value);
                 if($product_value){
                    $res->price=$product_value['price'];
                    $res->activity_price=$product_value['activity_price'];
                    $checked_skus=$product_value['sku'];
                }
                $res->checked_skus=$checked_skus;
                if($checked_skus){
                    $checked_image=Db::name('goods_value')->where('products_id',$data['products_id'])->where('sku',$checked_skus)->value('image');
                    if($checked_image){
                        $res->cover_picture=$checked_image;
                    }
                }
                $sp_temp=json_decode($res['specs_value'],true);
                unset($res['specs_value']);
                unset($res['skus']);
                $res['specs_value_temp']=$sp_temp;
        }
         $this->result('success',$res,200,'json');
    }
      public function get_check_sku($products_id,$new_specs_value){
        
        $item_tmp='';
        foreach ($new_specs_value as $k1 => $v1) {
            foreach ($v1['childern'] as $k2 => $v2) {
                if($v2['checked']==true){
                   $item_tmp.="_".$v2['id'];
                }
            }
         }
         $item_tmp= array_filter(explode('_',$item_tmp));
         sort($item_tmp);
         $item_tmp=implode('_',$item_tmp);
         $product_value=Db::name('goods_value')->where(['products_id'=>$products_id,'sku_val'=>$item_tmp])->find();
         if(empty($product_value)){
             $aa=Db::name('goods_value')->where(['products_id'=>$products_id])->select()->toArray();
             foreach ($aa as $k => $v) {
                 $sku_val=explode('_',$v['sku_val']);
                 sort($sku_val);
                 $sku_val=implode('_',$sku_val);
                 Db::name('goods_value')->where(['products_id'=>$products_id,'sku'=>$v['sku']])->update(['sku_val'=>$sku_val]);
             }
             $product_value=Db::name('goods_value')->where(['products_id'=>$products_id,'sku_val'=>$item_tmp])->find();
            
         }
         return $product_value;
    }
    public function getSpecData($specs,$specs_value,$products_id){
        
            $aa=Db::name('goods_spec')->field('id,title')->whereIn('id',$specs)->order('sort asc')->select()->toArray();
            $product_value=Db::name('goods_value')->where(['products_id'=>$products_id])->column('sku_val');
            $temp_sku=[];
            foreach ($product_value as $k => $v) {
                    $sku=explode('_',$v);
                    foreach ($sku as $key => $value) {
                        if(!empty($temp_sku)){
                            if(!in_array($value,$temp_sku))  array_push($temp_sku,$value);
                        } else{
                            array_push($temp_sku,$value);
                        }  
                       
                    }
            }
             
            foreach ($aa as $k => $v) {
           
                $aa[$k]['childern']=Db::name('goods_spec_item')->field('id,item')->whereIn('id',$temp_sku)->whereIn('item',$specs_value[$v['title']])->where('spec_id',$v['id'])->order('id asc')->select()->toArray();
              
            }
            foreach ($aa as $k => $v) {
                foreach ($v['childern'] as $kk => $vv) {
                    if($kk==0){
                        $aa[$k]['childern'][$kk]['checked']=true;
                    }else{
                        $aa[$k]['childern'][$kk]['checked']=false;
                    }
                    
                }
            }
           return $aa;
    }
      public function getProductSpecItem(){
        $data=input("get.");
     
        $spec_item=$data['spec_item'];
        $spec_item=explode('_',$spec_item);
        sort($spec_item);
        $spec_item=implode('_',$spec_item);
        $where=[
                ['products_id','=',$data['products_id']],
                ['sku_val','=',$spec_item]
              ];
        $data=Db::name('goods_value')->field('products_id,sku,price,member_price,activity_price,price_show,member_show,activity_show,image')->where($where)->find();
        $this->success('ok', $data,200,'json');
    }


}